package com.hhkj.admin.course.service.extend.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhkj.admin.course.common.cont.CourseConst;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.model.dopo.CourseDo;
import com.hhkj.admin.course.model.dopo.CoursePo;
import com.hhkj.admin.course.service.base.CourseService;
import com.hhkj.admin.course.service.extend.CourseExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * CourseServiceExtendImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 8:52
 */
@Service
public class CourseExtendServiceImpl
        extends BaseExtendServiceImpl<Course, CourseDo, CoursePo, CourseService>
        implements CourseExtendService
{

    @Override
    public List<Course> listAllWithStructureName()
    {
        List<Course> courseAllList = this.getService().list(Wrappers.<Course>lambdaQuery().eq(Course::getDisableFlag, 0));
        Map<Long, Course> courseMapById = courseAllList.stream()
                .collect(Collectors.toMap(Course::getId, Function.identity()));
        for (Course course : courseAllList) {
            setStructureName(course, courseMapById, course.getParentId());
        }
        return courseAllList.stream()
                .filter(coursePo -> CourseConst.LEVEL_NUM_THIRD.equals(coursePo.getLevelNum()))
                .collect(Collectors.toList());
    }

    /**
     * 课程结构名称设置
     *
     * @param course        课程
     * @param courseMapById 课程 id map
     * @param parentId      上级id
     */
    private void setStructureName(Course course, Map<Long, Course> courseMapById, Long parentId)
    {
        if (CourseConst.ROOT_PARENT_ID.equals(course.getParentId())) {
            course.setStructureName(course.getName());
            return;
        }
        if (StrUtil.isBlank(course.getStructureName())) {
            course.setStructureName(course.getName());
        }
        if (courseMapById.containsKey(parentId)) {
            Course parentCourse = courseMapById.get(parentId);
            if (CourseConst.ROOT_PARENT_ID.equals(parentCourse.getParentId())) {
                course.setStructureName(parentCourse.getName() + StrUtil.SLASH + course.getStructureName());
                return;
            }
            course.setStructureName(parentCourse.getName() + StrUtil.SLASH + course.getStructureName());
            setStructureName(course, courseMapById, parentCourse.getParentId());
        }
    }

    @Override
    public List<Course> listLevelNumThirdByParentIdNotDisable(Long parentId)
    {
        this.getLevelNumSecondByIdNotDisable(parentId);
        List<Course> courseChildren = this.listByParentId(parentId);
        if (CollectionUtil.isEmpty(courseChildren)) {
            throw new GlobalException("该类别下不存在启用的科目信息");
        }
        return courseChildren;
    }

    @Override
    public List<Course> listByParentId(Long parentId)
    {
        return this.listByParentId(parentId, SysConst.Flag.NO.getFlag());
    }

    @Override
    public List<Course> listByParentId(Long parentId, Integer disableFlag)
    {
        LambdaQueryWrapper<Course> courseQuery = new LambdaQueryWrapper<>();
        courseQuery.eq(Course::getParentId, parentId)
                .eq(Course::getDisableFlag, disableFlag);
        return this.getService().list(courseQuery);
    }

    @Override
    public Course getLevelNumSecondByIdNotDisable(Long id)
    {
        Course coursePo = this.getByIdNotDisable(id);
        if (!CourseConst.LEVEL_NUM_SECOND.equals(coursePo.getLevelNum())) {
            throw new GlobalException("类别信息错误");
        }
        return coursePo;
    }

    @Override
    public Course getLevelNumThirdByIdNotDisable(Long id)
    {
        Course coursePo = this.getByIdNotDisable(id);
        if (!CourseConst.LEVEL_NUM_THIRD.equals(coursePo.getLevelNum())) {
            throw new GlobalException("科目信息错误");
        }
        return coursePo;
    }

    @Override
    public Course getByIdNotDisable(Long id)
    {
        Course coursePo = this.getByIdNotNull(id);
        if (SysConst.Flag.IS.getFlag().equals(coursePo.getDisableFlag())) {
            throw new GlobalException("所选课程已禁用");
        }
        return coursePo;
    }

    /**
     * 查询全部未禁用的科目名称 （level_num=3）
     *
     * @param course 科目名称
     * @return 全部科目列表
     */
    @Override
    public List<Course> listPoAllCourseByDo(Course course)
    {
        return this.getService().list(Wrappers.<Course>lambdaQuery()
                .eq(Course::getDisableFlag, course.getDisableFlag())
                .eq(Course::getLevelNum, course.getLevelNum())
                .orderByDesc(Course::getCreateTime));
    }
}
